package com.noble.meether.chat.unread;

import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.log.PSP;
import com.noble.library.observe.ModuleBase;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;

import java.util.HashMap;
import java.util.Map;

/**
 * 未读角标实现类
 */
public class UnreadMgrImpl implements ModuleBase, PObserver {

    // =========================未读角标配置（添加新的角标类型后在此进行配置）============================

    /* --------一级角标：最顶层父级-------- */
    public static final String MAIL = "MAIL";                           //信箱
    public static final String CENTER = "CENTER";                       //个人中心

    /* --------二级角标-------- */
    public static final String MY_FRIEND = "MY_FRIEND";                 //好友申请
    public static final String MSG_NOTICE = "MSG_NOTICE";               //好友聊天
    public static final String SYS_NOTICE = "SYS_NOTICE";               //系统消息

    /* --------三级角标-------- */
    // 暂无
    /* --------游离角标-------- */
    // 暂无

    /* --------未读消息的级联关系，每次添加新的层级角标之后在此进行配置-------- */
    private static final Map<String, String[]> parentMap = new HashMap<String, String[]>() {
        {
            put(MY_FRIEND, new String[]{MAIL});
            put(MSG_NOTICE, new String[]{MAIL});
            put(SYS_NOTICE, new String[]{MAIL});
        }
    };

    // ====================================未读角标配置 end============================================

    @Override
    public void init() {
        MsgMgr.getInstance().attach(this);//监听登录消息
    }

    @Override
    public void release() {
        MsgMgr.getInstance().detach(this);
    }

    /**
     * @return 获取未读角标管理类
     */
    public UnreadMgr getUnreadMgr() {
        return UnreadMgr.getInstance();
    }

    /**
     * 获取角标数据存储的key，以uid进行存储，便于用户切换帐号处理
     *
     * @return 获取最终存储用户角标信息的标签
     */
    private String getStoreTag() {
        return "unread_a" + String.valueOf(ModuleMgr.getLoginMgr().getUid());
    }

    public static String getMsgStoreTag() {
        return "unread_msg_a" + String.valueOf(ModuleMgr.getLoginMgr().getUid());
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_App_Login:
                if (!(Boolean) value) return;
                // 初始化角标数据
                String storeString = PSP.getInstance().getString(getStoreTag(), "");
                getUnreadMgr().init(storeString, parentMap);
                getUnreadMgr().initMsgListUnread();  // 初始化消息未读

                // 角标变更监听，每次变更之后更新存储数据
                getUnreadMgr().setUnreadListener(new UnreadMgr.UnreadListener() {
                    @Override
                    public void onUnreadChange(String key, boolean isAdd, String storeString) {
                        PSP.getInstance().put(getStoreTag(), storeString);
                    }

                    @Override
                    public void onMsgUnreadChange(long key, String storeString) {
                        PSP.getInstance().put(getMsgStoreTag(), storeString);
                    }
                });
                break;
        }
    }
}
